﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WEIShareWCFRole.BusinessEntities.Paging;
using WEIShareWCFRole.BusinessEntities;

namespace WEIShareWCFRole.DataAccessLayer
{
    /// <summary>
    /// Summary description for WindowsAssessment
    /// </summary>
    public class WindowsAssessment
    {
        public class DataContextWindowsAssessment : DataContext.WindowsAssessment { }

        public static void Delete(Guid ID)
        {

            using (DataContext.DataClassesDataContext dataContext = new DataContext.DataClassesDataContext())
            {
                IQueryable<DataContext.WindowsAssessment> entity = dataContext.WindowsAssessments.Where(entityResult => entityResult.WindowsAssessmentID == ID);
                DataContext.WindowsAssessment entityToDelete = entity.FirstOrDefault();

                if (entityToDelete != null)
                {
                    dataContext.WindowsAssessments.DeleteOnSubmit(entityToDelete);
                    dataContext.SubmitChanges();
                }
            }

        }

        public static PageResponse<BusinessEntities.WindowsAssessment> GetPaged(PageRequest pageRequest, AdvancedSearch advancedSearch)
        {
            using (DataContext.DataClassesDataContext dataContext = new DataContext.DataClassesDataContext())
            {
                dataContext.ObjectTrackingEnabled = false;

                List<BusinessEntities.WindowsAssessment> windowsAssessments = new List<BusinessEntities.WindowsAssessment>();

                IQueryable<DataContextWindowsAssessment> windowsAssessmentListing;

                if (pageRequest.IsAdmin)
                {
                    windowsAssessmentListing = (from windowsAssessmentResults in dataContext.WindowsAssessments
                                                where windowsAssessmentResults.SystemScore.HasValue
                                                select new DataContextWindowsAssessment
                                                {
                                                    WindowsAssessmentID = windowsAssessmentResults.WindowsAssessmentID,
                                                    AverageScore = windowsAssessmentResults.AverageScore,
                                                    CpuScore = windowsAssessmentResults.CpuScore,
                                                    CpuSubAggScore = windowsAssessmentResults.CpuSubAggScore,
                                                    DiskScore = windowsAssessmentResults.DiskScore,
                                                    DiskSize = windowsAssessmentResults.DiskSize,
                                                    DiskSizeAvailable = windowsAssessmentResults.DiskSizeAvailable,
                                                    DiskSpeed = windowsAssessmentResults.DiskSpeed,
                                                    GamingScore = windowsAssessmentResults.GamingScore,
                                                    GraphicsAdapterDescription = windowsAssessmentResults.GraphicsAdapterDescription,
                                                    GraphicsAdapterManufacturer = windowsAssessmentResults.GraphicsAdapterManufacturer,
                                                    GraphicsDedicatedSystemMemory = windowsAssessmentResults.GraphicsDedicatedSystemMemory,
                                                    GraphicsDirectXVersion = windowsAssessmentResults.GraphicsDirectXVersion,
                                                    GraphicsScore = windowsAssessmentResults.GraphicsScore,
                                                    GraphicsSharedSystemMemory = windowsAssessmentResults.GraphicsSharedSystemMemory,
                                                    GraphicsSupports32BitsPerPixel = windowsAssessmentResults.GraphicsSupports32BitsPerPixel,
                                                    GraphicsDriverVersion = windowsAssessmentResults.GraphicsDriverVersion,
                                                    GraphicsDedicatedVideoMemory = windowsAssessmentResults.GraphicsDedicatedVideoMemory,
                                                    MemoryPhysicalSize = windowsAssessmentResults.MemoryPhysicalSize,
                                                    MemoryPhysicalSizeBytes = windowsAssessmentResults.MemoryPhysicalSizeBytes,
                                                    MemoryAvailablePhysicalSize = windowsAssessmentResults.MemoryAvailablePhysicalSize,
                                                    MemoryAvailablePhysicalSizeBytes = windowsAssessmentResults.MemoryAvailablePhysicalSizeBytes,
                                                    MemoryScore = windowsAssessmentResults.MemoryScore,
                                                    MemorySpeed = windowsAssessmentResults.MemorySpeed,
                                                    MemoryTypeID = windowsAssessmentResults.MemoryTypeID,
                                                    MemoryType = windowsAssessmentResults.MemoryType,
                                                    MonitorsCount = windowsAssessmentResults.MonitorsCount,
                                                    MonitorWidth = windowsAssessmentResults.MonitorWidth,
                                                    MonitorHeight = windowsAssessmentResults.MonitorHeight,
                                                    MonitorTotalPixels = windowsAssessmentResults.MonitorTotalPixels,
                                                    OSVersionBuild = windowsAssessmentResults.OSVersionBuild,
                                                    OSVersionProductName = windowsAssessmentResults.OSVersionProductName,
                                                    OSVersionProductType = windowsAssessmentResults.OSVersionProductType,
                                                    PlatformIsMobile = windowsAssessmentResults.PlatformIsMobile,
                                                    PlatformRole = windowsAssessmentResults.PlatformRole,
                                                    PlatformRoleDesc = windowsAssessmentResults.PlatformRoleDesc,
                                                    ProcessorFrequency = windowsAssessmentResults.ProcessorFrequency,
                                                    ProcessorL1CacheSize = windowsAssessmentResults.ProcessorL1CacheSize,
                                                    ProcessorL2CacheSize = windowsAssessmentResults.ProcessorL2CacheSize,
                                                    ProcessorName = windowsAssessmentResults.ProcessorName,
                                                    ProcessorNumProcs = windowsAssessmentResults.ProcessorNumProcs,
                                                    ProcessorNumCores = windowsAssessmentResults.ProcessorNumCores,
                                                    ProcessorNumCoresPerProcessor = windowsAssessmentResults.ProcessorNumCoresPerProcessor,
                                                    ProcessorNumCPUs = windowsAssessmentResults.ProcessorNumCPUs,
                                                    ProcessorNumCPUsPerCore = windowsAssessmentResults.ProcessorNumCPUsPerCore,
                                                    ProcessorX64Capable = windowsAssessmentResults.ProcessorX64Capable,
                                                    ProcessorX64Running = windowsAssessmentResults.ProcessorX64Running,
                                                    SystemManufacturer = windowsAssessmentResults.SystemManufacturer,
                                                    SystemModel = windowsAssessmentResults.SystemModel,
                                                    SystemScore = windowsAssessmentResults.SystemScore,
                                                    SystemType = windowsAssessmentResults.SystemType,
                                                    VideoEncodeScore = windowsAssessmentResults.VideoEncodeScore,
                                                    TouchPointCount = windowsAssessmentResults.TouchPointCount
                                                });
                }
                else
                {
                    if (pageRequest.GroupResults)
                    {
                        windowsAssessmentListing = (from windowsAssessmentResults in dataContext.WindowsAssessments
                                                    where windowsAssessmentResults.SystemScore.HasValue
                                                    select new DataContextWindowsAssessment
                                                    {
                                                        AverageScore = windowsAssessmentResults.AverageScore,
                                                        CpuScore = windowsAssessmentResults.CpuScore,
                                                        DiskScore = windowsAssessmentResults.DiskScore,
                                                        DiskSize = windowsAssessmentResults.DiskSize,
                                                        DiskSizeAvailable = windowsAssessmentResults.DiskSizeAvailable,
                                                        DiskSpeed = windowsAssessmentResults.DiskSpeed,
                                                        GamingScore = windowsAssessmentResults.GamingScore,
                                                        GraphicsAdapterDescription = windowsAssessmentResults.GraphicsAdapterDescription,
                                                        GraphicsAdapterManufacturer = windowsAssessmentResults.GraphicsAdapterManufacturer,
                                                        GraphicsDedicatedSystemMemory = windowsAssessmentResults.GraphicsDedicatedSystemMemory,
                                                        GraphicsDirectXVersion = windowsAssessmentResults.GraphicsDirectXVersion,
                                                        GraphicsDriverVersion = windowsAssessmentResults.GraphicsDriverVersion,
                                                        GraphicsScore = windowsAssessmentResults.GraphicsScore,
                                                        GraphicsSharedSystemMemory = windowsAssessmentResults.GraphicsSharedSystemMemory,
                                                        GraphicsDedicatedVideoMemory = windowsAssessmentResults.GraphicsDedicatedVideoMemory,
                                                        MemoryPhysicalSize = windowsAssessmentResults.MemoryPhysicalSize,
                                                        MemoryPhysicalSizeBytes = windowsAssessmentResults.MemoryPhysicalSizeBytes,
                                                        MemoryScore = windowsAssessmentResults.MemoryScore,
                                                        MemorySpeed = windowsAssessmentResults.MemorySpeed,
                                                        MemoryTypeID = windowsAssessmentResults.MemoryTypeID,
                                                        MemoryType = windowsAssessmentResults.MemoryType,
                                                        MonitorsCount = windowsAssessmentResults.MonitorsCount,
                                                        OSVersionProductName = windowsAssessmentResults.OSVersionProductName,
                                                        PlatformIsMobile = windowsAssessmentResults.PlatformIsMobile,
                                                        ProcessorFrequency = windowsAssessmentResults.ProcessorFrequency,
                                                        ProcessorL2CacheSize = windowsAssessmentResults.ProcessorL2CacheSize,
                                                        ProcessorName = windowsAssessmentResults.ProcessorName,
                                                        ProcessorNumCores = windowsAssessmentResults.ProcessorNumCores,
                                                        ProcessorX64Capable = windowsAssessmentResults.ProcessorX64Capable,
                                                        SystemManufacturer = windowsAssessmentResults.SystemManufacturer,
                                                        SystemModel = windowsAssessmentResults.SystemModel,
                                                        SystemScore = windowsAssessmentResults.SystemScore,
                                                        SystemType = windowsAssessmentResults.SystemType,
                                                        TouchPointCount = windowsAssessmentResults.TouchPointCount
                                                    }).Distinct();
                    }
                    else
                    {
                        windowsAssessmentListing = (from windowsAssessmentResults in dataContext.WindowsAssessments
                                                    where windowsAssessmentResults.SystemScore.HasValue
                                                    select new DataContextWindowsAssessment
                                                    {
                                                        WindowsAssessmentID = windowsAssessmentResults.WindowsAssessmentID,
                                                        AverageScore = windowsAssessmentResults.AverageScore,
                                                        CpuScore = windowsAssessmentResults.CpuScore,
                                                        DiskScore = windowsAssessmentResults.DiskScore,
                                                        DiskSize = windowsAssessmentResults.DiskSize,
                                                        DiskSizeAvailable = windowsAssessmentResults.DiskSizeAvailable,
                                                        DiskSpeed = windowsAssessmentResults.DiskSpeed,
                                                        GamingScore = windowsAssessmentResults.GamingScore,
                                                        GraphicsAdapterDescription = windowsAssessmentResults.GraphicsAdapterDescription,
                                                        GraphicsAdapterManufacturer = windowsAssessmentResults.GraphicsAdapterManufacturer,
                                                        GraphicsDedicatedSystemMemory = windowsAssessmentResults.GraphicsDedicatedSystemMemory,
                                                        GraphicsDirectXVersion = windowsAssessmentResults.GraphicsDirectXVersion,
                                                        GraphicsDriverVersion = windowsAssessmentResults.GraphicsDriverVersion,
                                                        GraphicsScore = windowsAssessmentResults.GraphicsScore,
                                                        GraphicsSharedSystemMemory = windowsAssessmentResults.GraphicsSharedSystemMemory,
                                                        GraphicsDedicatedVideoMemory = windowsAssessmentResults.GraphicsDedicatedVideoMemory,
                                                        MemoryPhysicalSize = windowsAssessmentResults.MemoryPhysicalSize,
                                                        MemoryPhysicalSizeBytes = windowsAssessmentResults.MemoryPhysicalSizeBytes,
                                                        MemoryScore = windowsAssessmentResults.MemoryScore,
                                                        MemorySpeed = windowsAssessmentResults.MemorySpeed,
                                                        MemoryTypeID = windowsAssessmentResults.MemoryTypeID,
                                                        MemoryType = windowsAssessmentResults.MemoryType,
                                                        MonitorsCount = windowsAssessmentResults.MonitorsCount,
                                                        OSVersionProductName = windowsAssessmentResults.OSVersionProductName,
                                                        PlatformIsMobile = windowsAssessmentResults.PlatformIsMobile,
                                                        ProcessorFrequency = windowsAssessmentResults.ProcessorFrequency,
                                                        ProcessorL2CacheSize = windowsAssessmentResults.ProcessorL2CacheSize,
                                                        ProcessorName = windowsAssessmentResults.ProcessorName,
                                                        ProcessorNumCores = windowsAssessmentResults.ProcessorNumCores,
                                                        ProcessorX64Capable = windowsAssessmentResults.ProcessorX64Capable,
                                                        SystemManufacturer = windowsAssessmentResults.SystemManufacturer,
                                                        SystemModel = windowsAssessmentResults.SystemModel,
                                                        SystemScore = windowsAssessmentResults.SystemScore,
                                                        SystemType = windowsAssessmentResults.SystemType,
                                                        TouchPointCount = windowsAssessmentResults.TouchPointCount
                                                    });
                    }
                }


                if (advancedSearch != null)
                {
                    windowsAssessmentListing = windowsAssessmentListing.Where(windowsAssessment =>
                                                                            (windowsAssessment.SystemScore >= advancedSearch.SystemScoreMin && windowsAssessment.SystemScore <= advancedSearch.SystemScoreMax) &&
                                                                            (windowsAssessment.ProcessorNumCores.GetValueOrDefault(0) >= advancedSearch.ProcessorNumCoresMin && windowsAssessment.ProcessorNumCores.GetValueOrDefault(0) <= advancedSearch.ProcessorNumCoresMax) &&
                                                                            (windowsAssessment.ProcessorL2CacheSize.GetValueOrDefault(0) >= advancedSearch.ProcessorL2CacheSizeMin && windowsAssessment.ProcessorL2CacheSize.GetValueOrDefault(0) <= advancedSearch.ProcessorL2CacheSizeMax) &&
                                                                            (windowsAssessment.MemoryPhysicalSizeBytes.GetValueOrDefault(0) >= advancedSearch.MemoryPhysicalSizeMin && windowsAssessment.MemoryPhysicalSizeBytes.GetValueOrDefault(0) <= advancedSearch.MemoryPhysicalSizeMax) &&
                                                                            (windowsAssessment.MemorySpeed.GetValueOrDefault(0) >= advancedSearch.MemorySpeedMin && windowsAssessment.MemorySpeed.GetValueOrDefault(0) <= advancedSearch.MemorySpeedMax) &&
                                                                            ((windowsAssessment.GraphicsDedicatedSystemMemory.GetValueOrDefault(0) + windowsAssessment.GraphicsDedicatedVideoMemory.GetValueOrDefault(0) + windowsAssessment.GraphicsSharedSystemMemory.GetValueOrDefault(0)) >= advancedSearch.GraphicsMemoryMin && (windowsAssessment.GraphicsDedicatedSystemMemory.GetValueOrDefault(0) + windowsAssessment.GraphicsDedicatedVideoMemory.GetValueOrDefault(0) + windowsAssessment.GraphicsSharedSystemMemory.GetValueOrDefault(0)) >= advancedSearch.GraphicsMemoryMin) &&
                                                                            (windowsAssessment.MonitorsCount.GetValueOrDefault(0) >= advancedSearch.MonitorsCountMin && windowsAssessment.MonitorsCount.GetValueOrDefault(0) <= advancedSearch.MonitorsCountMax) &&
                                                                            (windowsAssessment.DiskSize.GetValueOrDefault(0) >= advancedSearch.DiskSizeMin && windowsAssessment.DiskSize.GetValueOrDefault(0) <= advancedSearch.DiskSizeMax)
                                                                            );

                    if (!String.IsNullOrEmpty(advancedSearch.WindowsVersion))
                        windowsAssessmentListing = windowsAssessmentListing.Where(windosAssessment => windosAssessment.OSVersionProductName == advancedSearch.WindowsVersion);

                    if (!String.IsNullOrEmpty(advancedSearch.ProcessorName))
                        windowsAssessmentListing = windowsAssessmentListing.Where(windowsAssessment => windowsAssessment.ProcessorName.Contains(advancedSearch.ProcessorName));

                    if (advancedSearch.MemoryTypeID.HasValue)
                        windowsAssessmentListing = windowsAssessmentListing.Where(windowsAssessment => windowsAssessment.MemoryTypeID == advancedSearch.MemoryTypeID.Value);

                    if (!String.IsNullOrEmpty(advancedSearch.GraphicsAdapterDescription))
                        windowsAssessmentListing = windowsAssessmentListing.Where(windowsAssessment => windowsAssessment.GraphicsAdapterDescription.Contains(advancedSearch.GraphicsAdapterDescription));
                }

                if (!String.IsNullOrEmpty(pageRequest.SearchQuery))
                {
                    windowsAssessmentListing = windowsAssessmentListing.Where(windowsAssessment =>
                                                                            windowsAssessment.GraphicsAdapterDescription.Contains(pageRequest.SearchQuery) ||
                                                                            windowsAssessment.GraphicsAdapterManufacturer.Contains(pageRequest.SearchQuery) ||
                                                                            windowsAssessment.MemoryPhysicalSize.Contains(pageRequest.SearchQuery) ||
                                                                            windowsAssessment.OSVersionProductName.Contains(pageRequest.SearchQuery) ||
                                                                            windowsAssessment.ProcessorName.Contains(pageRequest.SearchQuery) ||
                                                                            windowsAssessment.SystemManufacturer.Contains(pageRequest.SearchQuery) ||
                                                                            windowsAssessment.SystemModel.Contains(pageRequest.SearchQuery) ||
                                                                            windowsAssessment.SystemType.Contains(pageRequest.SearchQuery));
                }

                if (pageRequest.Filter != null)
                {
                    if (pageRequest.Filter.Parameter.Equals("WindowsAssessmentID", StringComparison.OrdinalIgnoreCase))
                        windowsAssessmentListing = windowsAssessmentListing.Where(windowsAssessment => windowsAssessment.WindowsAssessmentID == new Guid(pageRequest.Filter.Value));
                    else if (pageRequest.Filter.Parameter.Equals("MACAddress", StringComparison.OrdinalIgnoreCase))
                        windowsAssessmentListing = windowsAssessmentListing.Where(windowsAssessment => windowsAssessment.MACAddress.ToLower() == pageRequest.Filter.Value.ToLower());
                }

                switch (pageRequest.OrderBy)
                {
                    case "SystemScore":
                        windowsAssessmentListing = pageRequest.OrderByDirection.Equals("ASC", StringComparison.OrdinalIgnoreCase)
                                                    ? windowsAssessmentListing.OrderBy(entity => entity.SystemScore)
                                                        .ThenBy(entity => entity.AverageScore)
                                                    : windowsAssessmentListing.OrderByDescending(entity => entity.SystemScore)
                                                        .ThenByDescending(entity => entity.AverageScore);
                        break;
                    case "CpuScore":
                        windowsAssessmentListing = pageRequest.OrderByDirection.Equals("ASC", StringComparison.OrdinalIgnoreCase)
                                                    ? windowsAssessmentListing.OrderBy(entity => entity.CpuScore)
                                                        .ThenBy(entity => entity.SystemScore)
                                                        .ThenBy(entity => entity.AverageScore)
                                                    : windowsAssessmentListing.OrderByDescending(entity => entity.CpuScore)
                                                        .ThenByDescending(entity => entity.SystemScore)
                                                        .ThenByDescending(entity => entity.AverageScore);
                        break;
                    case "MemoryScore":
                        windowsAssessmentListing = pageRequest.OrderByDirection.Equals("ASC", StringComparison.OrdinalIgnoreCase)
                                                    ? windowsAssessmentListing.OrderBy(entity => entity.MemoryScore)
                                                        .ThenBy(entity => entity.SystemScore)
                                                        .ThenBy(entity => entity.AverageScore)
                                                    : windowsAssessmentListing.OrderByDescending(entity => entity.MemoryScore)
                                                        .ThenByDescending(entity => entity.SystemScore)
                                                        .ThenByDescending(entity => entity.AverageScore);
                        break;
                    case "GraphicsScore":
                        windowsAssessmentListing = pageRequest.OrderByDirection.Equals("ASC", StringComparison.OrdinalIgnoreCase)
                                                    ? windowsAssessmentListing.OrderBy(entity => entity.GraphicsScore)
                                                        .ThenBy(entity => entity.SystemScore)
                                                        .ThenBy(entity => entity.AverageScore)
                                                    : windowsAssessmentListing.OrderByDescending(entity => entity.GraphicsScore)
                                                        .ThenByDescending(entity => entity.SystemScore)
                                                        .ThenByDescending(entity => entity.AverageScore);
                        break;
                    case "TouchPointCount":
                        windowsAssessmentListing = pageRequest.OrderByDirection.Equals("ASC", StringComparison.OrdinalIgnoreCase)
                                                    ? windowsAssessmentListing.OrderBy(entity => entity.TouchPointCount)
                                                        .ThenBy(entity => entity.SystemScore)
                                                        .ThenBy(entity => entity.AverageScore)
                                                    : windowsAssessmentListing.OrderByDescending(entity => entity.TouchPointCount)
                                                        .ThenByDescending(entity => entity.SystemScore)
                                                        .ThenByDescending(entity => entity.AverageScore);
                        break;
                    case "GamingScore":
                        windowsAssessmentListing = pageRequest.OrderByDirection.Equals("ASC", StringComparison.OrdinalIgnoreCase)
                                                    ? windowsAssessmentListing.OrderBy(entity => entity.GamingScore)
                                                        .ThenBy(entity => entity.SystemScore)
                                                        .ThenBy(entity => entity.AverageScore)
                                                    : windowsAssessmentListing.OrderByDescending(entity => entity.GamingScore)
                                                        .ThenByDescending(entity => entity.SystemScore)
                                                        .ThenByDescending(entity => entity.AverageScore);
                        break;
                    case "DiskScore":
                        windowsAssessmentListing = pageRequest.OrderByDirection.Equals("ASC", StringComparison.OrdinalIgnoreCase)
                                                    ? windowsAssessmentListing.OrderBy(entity => entity.DiskScore)
                                                        .ThenBy(entity => entity.SystemScore)
                                                        .ThenBy(entity => entity.AverageScore)
                                                    : windowsAssessmentListing.OrderByDescending(entity => entity.DiskScore)
                                                        .ThenByDescending(entity => entity.SystemScore)
                                                        .ThenByDescending(entity => entity.AverageScore);
                        break;
                    default:
                        break;
                }


                int results = windowsAssessmentListing.Count();
                int totalPages = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(results) / Convert.ToDouble(pageRequest.PageSize)));
                pageRequest.PageNumber = Math.Max(1, Math.Min(totalPages, pageRequest.PageNumber));

                IQueryable<DataContextWindowsAssessment> PagedData = windowsAssessmentListing.Skip((pageRequest.PageNumber - 1) * pageRequest.PageSize).Take(pageRequest.PageSize);

                windowsAssessments = ConstructObject(PagedData).ToList();


                return new PageResponse<BusinessEntities.WindowsAssessment>() { PageListing = windowsAssessments, TotalPages = totalPages, PageNumber = pageRequest.PageNumber };
            }
        }


        /// <summary>
        /// Determines whether or not the Windows Assessment submitted has been recorded before, based on Mac Address and Scores
        /// </summary>
        /// <param name="windowsAssessmentEntity">Windows Assessment</param>
        /// <returns>True or False</returns>
        public static Boolean WindowsAssessementAlreadyRecorded(BusinessEntities.WindowsAssessment windowsAssessmentEntity)
        {
            bool assessmentAlreadyRecorded = false;

            using (DataContext.DataClassesDataContext dataContext = new DataContext.DataClassesDataContext())
            {
                dataContext.ObjectTrackingEnabled = false;

                DataContext.WindowsAssessment windowsAssessment = new DataContext.WindowsAssessment();

                Guid windowsAssessmentID = (from results in dataContext.WindowsAssessments
                                            where results.MACAddress == windowsAssessmentEntity.MACAddress &&
                                                   results.SystemScore == windowsAssessmentEntity.SystemScore &&
                                                   results.MemoryScore == windowsAssessmentEntity.MemoryScore &&
                                                   results.CpuScore == windowsAssessmentEntity.CpuScore &&
                                                   results.GraphicsScore == windowsAssessmentEntity.GraphicsScore &&
                                                   results.GamingScore == windowsAssessmentEntity.GamingScore &&
                                                   results.DiskScore == windowsAssessmentEntity.DiskScore
                                            select results.WindowsAssessmentID).FirstOrDefault();

                if (windowsAssessmentID != Guid.Empty) assessmentAlreadyRecorded = true;

            }
            return assessmentAlreadyRecorded;
        }

        public static Guid Save(BusinessEntities.WindowsAssessment windowsAssessmentEntity)
        {
            using (DataContext.DataClassesDataContext dataContext = new DataContext.DataClassesDataContext())
            {
                bool insertEntity = false;

                DataContext.WindowsAssessment windowsAssessment = new DataContext.WindowsAssessment();


                if (windowsAssessmentEntity.WindowsAssessmentID.HasValue && windowsAssessmentEntity.WindowsAssessmentID.Value != Guid.Empty)
                {
                    windowsAssessment = (from results in dataContext.WindowsAssessments
                                         where results.WindowsAssessmentID == windowsAssessmentEntity.WindowsAssessmentID
                                         select results).FirstOrDefault();

                    if (windowsAssessment == null) insertEntity = true;

                }
                else
                {
                    insertEntity = true;
                    windowsAssessmentEntity.WindowsAssessmentID = Guid.NewGuid();
                }

                if (insertEntity)
                {
                    windowsAssessment = new DataContext.WindowsAssessment();
                    dataContext.WindowsAssessments.InsertOnSubmit(windowsAssessment);

                }

                if (windowsAssessmentEntity.MemoryTypeID.HasValue && (windowsAssessmentEntity.MemoryTypeID.Value < 0 || windowsAssessmentEntity.MemoryTypeID.Value > 21))
                {
                    windowsAssessmentEntity.MemoryTypeID = 0;
                }

                windowsAssessment.WindowsAssessmentID = windowsAssessmentEntity.WindowsAssessmentID.Value;
                windowsAssessment.AverageScore = (windowsAssessmentEntity.CpuScore.GetValueOrDefault(0) + windowsAssessmentEntity.MemoryScore.GetValueOrDefault(0) + windowsAssessmentEntity.GraphicsScore.GetValueOrDefault(0) + windowsAssessmentEntity.GamingScore.GetValueOrDefault(0) + windowsAssessmentEntity.DiskScore.GetValueOrDefault(0)) / 5;
                windowsAssessment.CpuScore = windowsAssessmentEntity.CpuScore;
                windowsAssessment.CpuSubAggScore = windowsAssessmentEntity.CpuSubAggScore;
                windowsAssessment.DiskScore = windowsAssessmentEntity.DiskScore;
                windowsAssessment.DiskSize = windowsAssessmentEntity.DiskSize;
                windowsAssessment.DiskSizeAvailable = windowsAssessmentEntity.DiskSizeAvailable;
                windowsAssessment.DiskSpeed = windowsAssessmentEntity.DiskSpeed;
                windowsAssessment.GamingScore = windowsAssessmentEntity.GamingScore;
                windowsAssessment.GraphicsAdapterDescription = windowsAssessmentEntity.GraphicsAdapterDescription != null ? windowsAssessmentEntity.GraphicsAdapterDescription.Trim() : "";
                windowsAssessment.GraphicsAdapterManufacturer = windowsAssessmentEntity.GraphicsAdapterManufacturer != null ? windowsAssessmentEntity.GraphicsAdapterManufacturer.Trim() : "";
                windowsAssessment.GraphicsDedicatedSystemMemory = windowsAssessmentEntity.GraphicsDedicatedSystemMemory;
                windowsAssessment.GraphicsDedicatedVideoMemory = windowsAssessmentEntity.GraphicsDedicatedVideoMemory;
                windowsAssessment.GraphicsDriverVersion = windowsAssessmentEntity.GraphicsDriverVersion != null ? windowsAssessmentEntity.GraphicsDriverVersion.Trim() : "";
                windowsAssessment.GraphicsDirectXVersion = windowsAssessmentEntity.GraphicsDirectXVersion != null ? windowsAssessmentEntity.GraphicsDirectXVersion.Trim() : "";
                windowsAssessment.GraphicsScore = windowsAssessmentEntity.GraphicsScore;
                windowsAssessment.GraphicsSharedSystemMemory = windowsAssessmentEntity.GraphicsSharedSystemMemory;
                windowsAssessment.GraphicsSupports32BitsPerPixel = windowsAssessmentEntity.GraphicsSupports32BitsPerPixel;
                windowsAssessment.MACAddress = windowsAssessmentEntity.MACAddress != null ? windowsAssessmentEntity.MACAddress.Trim() : "";
                windowsAssessment.MemoryAvailablePhysicalSize = windowsAssessmentEntity.MemoryAvailablePhysicalSize != null ? windowsAssessmentEntity.MemoryAvailablePhysicalSize.Trim() : "";
                windowsAssessment.MemoryAvailablePhysicalSizeBytes = windowsAssessmentEntity.MemoryAvailablePhysicalSizeBytes;
                windowsAssessment.MemoryPhysicalSize = windowsAssessmentEntity.MemoryPhysicalSize != null ? windowsAssessmentEntity.MemoryPhysicalSize.Trim() : "";
                windowsAssessment.MemoryPhysicalSizeBytes = windowsAssessmentEntity.MemoryPhysicalSizeBytes;
                windowsAssessment.MemoryScore = windowsAssessmentEntity.MemoryScore;
                windowsAssessment.MemorySpeed = windowsAssessmentEntity.MemorySpeed;
                windowsAssessment.MemoryTypeID = windowsAssessmentEntity.MemoryTypeID;
                windowsAssessment.MonitorHeight = windowsAssessmentEntity.MonitorHeight;
                windowsAssessment.MonitorsCount = windowsAssessmentEntity.MonitorsCount;
                windowsAssessment.MonitorTotalPixels = windowsAssessmentEntity.MonitorTotalPixels;
                windowsAssessment.MonitorWidth = windowsAssessmentEntity.MonitorWidth;
                windowsAssessment.OSVersionBuild = windowsAssessmentEntity.OSVersionBuild != null ? windowsAssessmentEntity.OSVersionBuild.Trim() : "";
                windowsAssessment.OSVersionProductName = windowsAssessmentEntity.OSVersionProductName != null ? windowsAssessmentEntity.OSVersionProductName.Trim() : "";
                windowsAssessment.OSVersionProductType = windowsAssessmentEntity.OSVersionProductType != null ? windowsAssessmentEntity.OSVersionProductType.Trim() : "";
                windowsAssessment.PlatformIsMobile = windowsAssessmentEntity.PlatformIsMobile;
                windowsAssessment.PlatformRole = windowsAssessmentEntity.PlatformRole != null ? windowsAssessmentEntity.PlatformRole.Trim() : "";
                windowsAssessment.PlatformRoleDesc = windowsAssessmentEntity.PlatformRoleDesc != null ? windowsAssessmentEntity.PlatformRoleDesc.Trim() : "";
                windowsAssessment.ProcessorFrequency = windowsAssessmentEntity.ProcessorFrequency;
                windowsAssessment.ProcessorL1CacheSize = windowsAssessmentEntity.ProcessorL1CacheSize;
                windowsAssessment.ProcessorL2CacheSize = windowsAssessmentEntity.ProcessorL2CacheSize;
                windowsAssessment.ProcessorName = windowsAssessmentEntity.ProcessorName != null ? windowsAssessmentEntity.ProcessorName.Trim() : "";
                windowsAssessment.ProcessorNumCores = windowsAssessmentEntity.ProcessorNumCores;
                windowsAssessment.ProcessorNumCoresPerProcessor = windowsAssessmentEntity.ProcessorNumCoresPerProcessor;
                windowsAssessment.ProcessorNumCPUs = windowsAssessmentEntity.ProcessorNumCPUs;
                windowsAssessment.ProcessorNumCPUsPerCore = windowsAssessmentEntity.ProcessorNumCPUsPerCore;
                windowsAssessment.ProcessorNumProcs = windowsAssessmentEntity.ProcessorNumProcs;
                windowsAssessment.ProcessorX64Capable = windowsAssessmentEntity.ProcessorX64Capable;
                windowsAssessment.ProcessorX64Running = windowsAssessmentEntity.ProcessorX64Running;
                windowsAssessment.SystemManufacturer = windowsAssessmentEntity.SystemManufacturer != null ? windowsAssessmentEntity.SystemManufacturer.Trim() : "";
                windowsAssessment.SystemModel = windowsAssessmentEntity.SystemModel != null ? windowsAssessmentEntity.SystemModel.Trim() : "";
                windowsAssessment.SystemScore = windowsAssessmentEntity.SystemScore;
                windowsAssessment.SystemType = windowsAssessmentEntity.SystemType != null ? windowsAssessmentEntity.SystemType.Trim() : "";
                windowsAssessment.VideoEncodeScore = windowsAssessmentEntity.VideoEncodeScore;
                windowsAssessment.DateSubmitted = DateTime.Now;
                windowsAssessment.TouchPointCount = windowsAssessmentEntity.TouchPointCount;

                dataContext.SubmitChanges();

                return windowsAssessment.WindowsAssessmentID;

            }
        }


        private static IQueryable<BusinessEntities.WindowsAssessment> ConstructObject(IQueryable<DataContextWindowsAssessment> windowsAssessmentData)
        {

            IQueryable<BusinessEntities.WindowsAssessment> windowsAssessmentEntities = from results in windowsAssessmentData
                                                                                       select new BusinessEntities.WindowsAssessment
                                                                                       {

                                                                                           WindowsAssessmentID = results.WindowsAssessmentID,
                                                                                           AverageScore = results.AverageScore,
                                                                                           CpuScore = results.CpuScore,
                                                                                           CpuSubAggScore = results.CpuSubAggScore,
                                                                                           DiskScore = results.DiskScore,
                                                                                           DiskSize = results.DiskSize,
                                                                                           DiskSizeAvailable = results.DiskSizeAvailable,
                                                                                           DiskSpeed = results.DiskSpeed,
                                                                                           GamingScore = results.GamingScore,
                                                                                           GraphicsAdapterDescription = results.GraphicsAdapterDescription,
                                                                                           GraphicsAdapterManufacturer = results.GraphicsAdapterManufacturer,
                                                                                           GraphicsDedicatedSystemMemory = results.GraphicsDedicatedSystemMemory,
                                                                                           GraphicsDedicatedVideoMemory = results.GraphicsDedicatedVideoMemory,
                                                                                           GraphicsDriverVersion = results.GraphicsDriverVersion,
                                                                                           GraphicsDirectXVersion = results.GraphicsDirectXVersion,
                                                                                           GraphicsScore = results.GraphicsScore,
                                                                                           GraphicsSharedSystemMemory = results.GraphicsSharedSystemMemory,
                                                                                           GraphicsSupports32BitsPerPixel = results.GraphicsSupports32BitsPerPixel,
                                                                                           MACAddress = results.MACAddress,
                                                                                           MemoryAvailablePhysicalSize = results.MemoryAvailablePhysicalSize,
                                                                                           MemoryAvailablePhysicalSizeBytes = results.MemoryAvailablePhysicalSizeBytes,
                                                                                           MemoryPhysicalSize = results.MemoryPhysicalSize,
                                                                                           MemoryPhysicalSizeBytes = results.MemoryPhysicalSizeBytes,
                                                                                           MemoryScore = results.MemoryScore,
                                                                                           MemorySpeed = results.MemorySpeed,
                                                                                           MemoryTypeID = results.MemoryTypeID,
                                                                                           MemoryType = results.MemoryType.Name,
                                                                                           MonitorHeight = results.MonitorHeight,
                                                                                           MonitorsCount = results.MonitorsCount,
                                                                                           MonitorTotalPixels = results.MonitorTotalPixels,
                                                                                           MonitorWidth = results.MonitorWidth,
                                                                                           OSVersionBuild = results.OSVersionBuild,
                                                                                           OSVersionProductName = results.OSVersionProductName,
                                                                                           OSVersionProductType = results.OSVersionProductType,
                                                                                           PlatformIsMobile = results.PlatformIsMobile,
                                                                                           PlatformRole = results.PlatformRole,
                                                                                           PlatformRoleDesc = results.PlatformRoleDesc,
                                                                                           ProcessorFrequency = results.ProcessorFrequency,
                                                                                           ProcessorL1CacheSize = results.ProcessorL1CacheSize,
                                                                                           ProcessorL2CacheSize = results.ProcessorL2CacheSize,
                                                                                           ProcessorName = results.ProcessorName,
                                                                                           ProcessorNumCores = results.ProcessorNumCores,
                                                                                           ProcessorNumCoresPerProcessor = results.ProcessorNumCoresPerProcessor,
                                                                                           ProcessorNumCPUs = results.ProcessorNumCPUs,
                                                                                           ProcessorNumCPUsPerCore = results.ProcessorNumCPUsPerCore,
                                                                                           ProcessorNumProcs = results.ProcessorNumProcs,
                                                                                           ProcessorX64Capable = results.ProcessorX64Capable,
                                                                                           ProcessorX64Running = results.ProcessorX64Running,
                                                                                           SystemManufacturer = results.SystemManufacturer,
                                                                                           SystemModel = results.SystemModel,
                                                                                           SystemScore = results.SystemScore,
                                                                                           SystemType = results.SystemType,
                                                                                           VideoEncodeScore = results.VideoEncodeScore,
                                                                                           TouchPointCount = results.TouchPointCount
                                                                                       };


            return windowsAssessmentEntities;


        }


    }
}